/*
 * ScrapLotAction.java
 *
 * Created on June 9, 2002, 12:54 AM
 */

package com.scalar.chiptrack.operations.process.actions;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.dao.WorkflowDBManager;
import com.scalar.chiptrack.operations.process.forms.LotTraceForm;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.users.User;

/**
 *
 * @author  Mallikarjun
 * @version 1.0
 */
public class LotTraceAction extends ChipTrackAction{
       private static final String ACTION_NAME = "/lotTrace.do";
       public ActionForward execute (ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
                                      throws Exception {

            LotTraceForm trace = (LotTraceForm)form;

			String lotNumber = trace.getLotNumber();
            String lotType   = trace.getLotType();
            String lot_id = request.getParameter("lotId");
            String psType = request.getParameter("PsType");

            HashMap errors = new HashMap();
            HttpSession session = request.getSession ();
            User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
            if ( user == null )
            {
                session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString () );
                return ( mapping.findForward ( "login" ) );
            }

            if ( lotNumber != null )
            {
                lotNumber = lotNumber.trim();

                request.setAttribute("lotNumber", lotNumber);
                request.setAttribute("lotType", lotType);
                LinkedHashMap traceMap =  WorkflowDBManager.getLotgenealogy( lotNumber, lotType );
                if ( traceMap.size() > 0 )
                {
                    request.setAttribute("traceMap", traceMap );
                    return mapping.findForward("success");
                }
                else
                {
                    if ( lotNumber != null )
                    {
                        errors.put("Error" , "No Lots found for the Lot Number - "+lotNumber);
                        request.setAttribute(SessionKeys.ERROR_MESSAGES, errors);
                        request.setAttribute( "lotNumber", lotNumber );
                    }
                    return mapping.findForward("failure");
                }
            }
           
            if ( lot_id != null )
            {
                try
                {
                    ArrayList lotHistoryList = null;

                    lotHistoryList = DataEntryDAO.getAllLotHistory(lot_id, psType);
                    request.setAttribute( "requestFrom", "lotTrace" );
                    request.setAttribute( "LotsList", lotHistoryList );
                }
                catch( Exception e )
                {
                    e.printStackTrace();
                    errors.put( "Could not load Lot History", null );
                }

                return mapping.findForward("lotHistory");

            }
            return mapping.findForward("failure");
       }
}
